//
//  LoginViewController.m
//  AAIM
//
//  Created by awh on 2019/11/24.
//  Copyright © 2019年 AAIM. All rights reserved.
//

#import "LoginViewController.h"
#import "ForgetPasswordViewController.h"
#import "RegisteredViewController.h"

#import "ConversationListDataBase.h"
#import "ChatMessagesDataBase.h"
#import "GroupMessageDataBase.h"
#import "ConversationManager.h"
#import "PublicMessageDataBase.h"
#import <CloudPushSDK/CloudPushSDK.h>
@interface LoginViewController ()<UIGestureRecognizerDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UIImageView * logoImageView;
@property (nonatomic,strong) UITextField * accountTF;
@property (nonatomic,strong) UITextField * passwordTF;
@property (nonatomic,strong) UIButton * loginBtn;
@property (nonatomic,strong) UIButton * useCodeLogin;
@property (nonatomic,strong) UIButton * forgetPassword;
@property (nonatomic,strong) UILabel * toRegistered;
@property (nonatomic,copy) NSString * account;
@property (nonatomic,copy)NSString * isCodeLogin;//0是不显示验证码登录，1显示
@property (nonatomic,strong)UIImageView * experienceImg;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KHEXCOLOR(@"ffffff");
    [self.view addSubview:self.logoImageView];
    [self.view addSubview:self.accountTF];
    [self.view addSubview:self.passwordTF];
    _isCodeLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"isVcode"];

    [self.view addSubview:self.useCodeLogin];
     [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.forgetPassword];
    [self.view addSubview:self.toRegistered];
    NSString * userAccout = [[NSUserDefaults standardUserDefaults]objectForKey:@"userAccout"];
    NSString * password = [[NSUserDefaults standardUserDefaults]objectForKey:@"userPassword"];
    if (userAccout) {
        _accountTF.text = userAccout;
        _passwordTF.text = password;
    }
    if (_isCodeLogin) {
    if ([_isCodeLogin isEqualToString:@"0"]) {
        _useCodeLogin.hidden = YES;
    }
    }else{
        [LoginRequestLogic getAppInfoUrlwithSuccess:^(NSDictionary * _Nonnull Dict) {
                       NSLog(@"app信息：%@",Dict);
               if ([Dict[@"code"] integerValue] == 200) {
                   KWeakSelf;

                   NSArray * dataAry = Dict[@"data"][@"configs"];
                   for (NSDictionary * dic in dataAry) {
                       if ([dic[@"attribute"] isEqualToString:@"isVcode"]) {
                           if ([dic[@"values"] isEqualToString:@"0"]) {
                               weakSelf.useCodeLogin.hidden = YES;
                           }else{
                              weakSelf.useCodeLogin.hidden = NO;
                           }
                       }
                   }

               }
           } failure:^(NSString * _Nonnull errMsg) {
               NSLog(@"请求配置失败");
           }];
    }
    [self setUI];
    
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.gk_navigationBar.gk_navLineHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.gk_navigationBar.gk_navLineHidden = NO;
}

- (void)setUI{
    [_logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_top).offset(NavigationBarHeight+20);
        make.width.offset(SCALAE_WIDTH(119.f));
        make.height.offset(SCALAE_WIDTH(119.f));
    }];
    [_accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(self.logoImageView.mas_bottom).offset(55.f);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(51.f);
    }];
    UIView * line1 = [UIView new];
    line1.backgroundColor = KHEXCOLOR(@"e0e0e0");
    [self.view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.top.equalTo(self.accountTF.mas_bottom);
        make.height.offset(0.5f);
    }];
    [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(line1.mas_bottom);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(51.f);
    }];
    
    UIView * line2 = [UIView new];
    line2.backgroundColor = KHEXCOLOR(@"e0e0e0");
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.top.equalTo(self.passwordTF.mas_bottom);
        make.height.offset(0.5f);
    }];
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(line2.mas_bottom).offset(26.f);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(40.f);
    }];
    [_useCodeLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(self.loginBtn.mas_bottom).offset(8.f);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-281.f));
    }];
    [_forgetPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginBtn.mas_bottom).offset(8.f);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
    }];
    
    [_toRegistered mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20.f);
    }];
    NSString * isFirstLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstLogin"];
    if ([isFirstLogin isEqualToString:@"0"] ||[CommonTools isEmpty:isFirstLogin]) {
        [[UIApplication sharedApplication].keyWindow addSubview:self.experienceImg];
    }
   
}

#pragma mark -- UITextFiledDelegate
- (void)passwardTFStrChange:(UITextField *)textFiled{
    NSLog(@"%@",textFiled.text);
}

- (void)accountTFStrChange:(UITextField *)textFiled{
    NSLog(@"%@",textFiled.text);
}

//登录
- (void)loginTouch:(UIButton *)sender{
    
    if ([NSObject ldy_isEmpty:_accountTF.text] ) {
        [self showTipHudWithText:@"账号不能为空"];
        return;
    }else if ([NSObject ldy_isEmpty:_passwordTF.text]){
        [self showTipHudWithText:@"密码不能为空"];
        return;
    }
    [self showAlwaysTipHudWithText:@"登录中，请稍等"];
    [LoginRequestLogic UserLoginUserName:_accountTF.text withType:1 withPassword:_passwordTF.text userUUid:[GetUUID getUUID] invitationCode:@"" Success:^(NSDictionary * _Nonnull Dict) {

        [self hideHud];
        NSLog(@"成功%@",Dict);
        if ([Dict[@"code"] integerValue] == 200) {
            NSDictionary * dic = Dict[@"data"];
            NSDictionary * userInfoDic = dic[@"userInfo"];
            UserModel * model = [UserModel mj_objectWithKeyValues:userInfoDic];
            model.token = dic[@"token"];
            model.uuid= [CloudPushSDK getDeviceId];
//            [[UIDevice currentDevice].identifierForVendor UUIDString];
            [[LoginManager shareLoginManager] saveLoginCustomer:model];
             [self showTipHudWithText:Dict[@"message"]];

            [[NSUserDefaults standardUserDefaults] setObject:self.accountTF.text forKey:@"userAccout"];
            [[NSUserDefaults standardUserDefaults] setObject:self.passwordTF.text forKey:@"userPassword"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            
            [self creatFile];
            // 打开数据库
            [[ConversationListDataBase shareConversationListDataBase] createTable];
            [[ChatMessagesDataBase shareChatMessagesDataBase] creatChatTable];
            [[GroupMessageDataBase shareGroupMessageDataBase] creatChatTable];
            [[PublicMessageDataBase sharePublicMessageDataBase] creatPublicTable];
            [[SystemDataBase shareSystemDataBase] createTable];
            [[ConversationManager shareManager] reloadData];
            //链接socket
            [[LiftIMClient sharedClient] connectWithUserid:1];
            [self saveFriendsWithGroup];
        }else{
             [self showTipHudWithText:Dict[@"message"]];
        }
    } failure:^(NSString * _Nonnull errMsg) {
         [self hideHud];
        if (errMsg) {
            [self showTipHudWithText:errMsg];
        }else{
            [self showTipHudWithText:@"登录失败"];
        }
        
         NSLog(@"错误%@",errMsg);
    }];
}
-(void)saveFriendsWithGroup
{
    [[UserDataBaseTool shareUserDataBaseTool] createTable];
    [[UserDataBaseTool shareUserDataBaseTool] SaveFriendsWithGroupsDataSuccess:^(BOOL isOK){
        [[LoginManager shareLoginManager] pushHomeViewController];
    }];
}
//使用验证码登录
- (void)useCodeTouch:(UIButton *)sender{
    NSLog(@"验证码登录");
    CodeLoginViewController * codeLoginVC = [[CodeLoginViewController alloc]init];
    
    [self.navigationController pushViewController:codeLoginVC animated:YES];
}

//忘记密码
- (void)forgetTouch:(UIButton *)sender{
    NSLog(@"忘记密码");
    ForgetPasswordViewController * forgetVC = [[ForgetPasswordViewController alloc]init];
    [self.navigationController pushViewController:forgetVC animated:YES];
}

//去注册
- (void)toRegisteredActive{
    RegisteredViewController * reginsVC = [[RegisteredViewController alloc]init];
    KWeakSelf;
    reginsVC.RegiserAccountBlock = ^(NSString * _Nonnull account) {
        weakSelf.accountTF.text = account;
    };
    [self.navigationController pushViewController:reginsVC animated:YES];
}

- (UIImageView *)logoImageView{
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc]init];
        _logoImageView.layer.cornerRadius = 5;
        _logoImageView.layer.masksToBounds = YES;
        _logoImageView.image = [UIImage imageNamed:@"logo"];
    }
    return _logoImageView;
}

- (UITextField *)accountTF{
    if (!_accountTF) {
        _accountTF = [[UITextField alloc]init];
        _accountTF.placeholder = @"用户名/邮箱/手机号";
        _accountTF.font = kMainFont(15);
        _accountTF.delegate =self;
        _accountTF.textColor = KHEXCOLOR(@"#262B2F");
        _accountTF.borderStyle = UITextBorderStyleNone;
        [_accountTF addTarget:self action:@selector(accountTFStrChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _accountTF;
}

- (UITextField *)passwordTF{
    if (!_passwordTF) {
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.placeholder = @"密码";
        _passwordTF.font = kMainFont(15);
        _passwordTF.delegate =self;
        _passwordTF.textColor = KHEXCOLOR(@"#262B2F");
        _passwordTF.borderStyle = UITextBorderStyleNone;
        _passwordTF.secureTextEntry = YES;
        [_passwordTF addTarget:self action:@selector(passwardTFStrChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _passwordTF;
}

-(UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setBackgroundColor:KHEXCOLOR(@"#4795EF")];
        _loginBtn.layer.cornerRadius = 5.0f;
        _loginBtn.layer.masksToBounds = YES;
        [_loginBtn setTitleColor:KHEXCOLOR(@"#ffffff") forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginTouch:) forControlEvents:UIControlEventTouchUpInside];
        [_loginBtn.titleLabel setFont:kMainFont(15)];
    }
    return _loginBtn;
}

-(UIButton *)useCodeLogin{
    if (!_useCodeLogin) {
        _useCodeLogin = [UIButton buttonWithType:UIButtonTypeCustom];
        [_useCodeLogin setTitle:@"验证码登录" forState:UIControlStateNormal];
        [_useCodeLogin setBackgroundColor:KHEXCOLOR(@"#FFFFFF")];
        [_useCodeLogin setTitleColor:KHEXCOLOR(@"#AFAFAF") forState:UIControlStateNormal];
        [_useCodeLogin addTarget:self action:@selector(useCodeTouch:) forControlEvents:UIControlEventTouchUpInside];
        [_useCodeLogin.titleLabel setFont:kMainFont(12)];
    }
    return _useCodeLogin;
}

-(UIButton *)forgetPassword{
    if (!_forgetPassword) {
        _forgetPassword = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forgetPassword setTitle:@"忘记密码" forState:UIControlStateNormal];
        [_forgetPassword setBackgroundColor:KHEXCOLOR(@"#FFFFFF")];
        [_forgetPassword setTitleColor:KHEXCOLOR(@"#AFAFAF") forState:UIControlStateNormal];
        [_forgetPassword addTarget:self action:@selector(forgetTouch:) forControlEvents:UIControlEventTouchUpInside];
        [_forgetPassword.titleLabel setFont:kMainFont(12)];
    }
    return _forgetPassword;
}

- (UILabel *)toRegistered{
    
    if (!_toRegistered) {
        _toRegistered = [[UILabel alloc]init];
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"还没有账号？立即注册"attributes: @{NSFontAttributeName: kMainFont(14),NSForegroundColorAttributeName: [UIColor colorWithRed:175/255.0 green:175/255.0 blue:175/255.0 alpha:1.0]}];
        
        [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:175/255.0 green:175/255.0 blue:175/255.0 alpha:1.000000]} range:NSMakeRange(0, 6)];
        
        [string addAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:38/255.0 green:43/255.0 blue:47/255.0 alpha:1.000000]} range:NSMakeRange(6, 4)];
        
        _toRegistered.attributedText = string;
        _toRegistered.userInteractionEnabled = YES;
        UITapGestureRecognizer * tapTouch = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toRegisteredActive)];
        tapTouch.numberOfTapsRequired = 1;
        tapTouch.numberOfTouchesRequired = 1;
        [_toRegistered addGestureRecognizer:tapTouch];
    }
    return _toRegistered;
}


// 沙盒创建图片和语音文件夹
- (void)creatFile {
    NSString * docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *imagePath = [docsdir stringByAppendingPathComponent:kMessageImageFileName];
    NSString *VoicePath = [docsdir stringByAppendingPathComponent:kMessageVoiceFileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL isDir = NO;
    BOOL isDir2 = NO;
    BOOL existedImagePath = [fileManager fileExistsAtPath:imagePath isDirectory:&isDir];
    BOOL existedVoicePath = [fileManager fileExistsAtPath:VoicePath isDirectory:&isDir2];
    
    if (!(isDir && existedImagePath)) {
        [fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    if (!(isDir2 && existedVoicePath)) {
        [fileManager createDirectoryAtPath:VoicePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

- (void)hideExproenceImgTouch:(UIButton *)sender{
    self.experienceImg.hidden = YES;
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isFirstLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (UIImageView *)experienceImg{
    if (!_experienceImg) {
        _experienceImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _experienceImg.image = [UIImage imageNamed:@"立即体验"];
        _experienceImg.userInteractionEnabled = YES;
        UIButton * beginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        beginBtn.frame = CGRectMake(0, kScreenHeight-60-42-EMVIEWBOTTOMMARGIN, SCALAE_WIDTH(261.f), 55);
        beginBtn.centerX = self.view.centerX;
        beginBtn.backgroundColor = [UIColor clearColor];
        [beginBtn addTarget:self action:@selector(hideExproenceImgTouch:) forControlEvents:UIControlEventTouchUpInside];
        [_experienceImg addSubview:beginBtn];
    }
    return _experienceImg;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end


@interface CodeLoginViewController ()<UIGestureRecognizerDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UIImageView * logoImageView;
@property (nonatomic,strong) UITextField * accountTF;
@property (nonatomic,strong) UITextField * passwordTF;
@property (nonatomic,strong) UIButton * loginBtn;
@property (nonatomic,strong) UIButton * getCodeBtn;
@property (nonatomic,strong) UITextField * inviteTF;
@property (nonatomic,strong) UIButton * backBtn;
@end

@implementation CodeLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KHEXCOLOR(@"ffffff");
    [self.view addSubview:self.logoImageView];
    [self.view addSubview:self.getCodeBtn];
    [self.view addSubview:self.accountTF];
    [self.view addSubview:self.passwordTF];
    [self.view addSubview:self.inviteTF];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.backBtn];
    [self setUI];


    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.gk_navigationBar.gk_navLineHidden = YES;
    
}
- (void)setUI{
    [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(self.view.mas_top).offset(34.f);
    }];
    [_logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_top).offset(38.f+kTopHeight);
        make.width.offset(SCALAE_WIDTH(119.f));
        make.height.offset(SCALAE_WIDTH(119.f));
    }];

    [_getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.centerY.equalTo(self.accountTF.mas_centerY);
        make.width.offset(SCALAE_WIDTH(100.f));
    }];
    [_accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(self.logoImageView.mas_bottom).offset(55.f);
        make.right.equalTo(self.getCodeBtn.mas_left).offset(SCALAE_WIDTH(-18.f));
        make.height.offset(51.f);
    }];
    UIView * line1 = [UIView new];
    line1.backgroundColor = KHEXCOLOR(@"#E0E0E0");
    [self.view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.top.equalTo(self.accountTF.mas_bottom);
        make.height.offset(0.5f);
    }];
    [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(line1.mas_bottom);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(51.f);
    }];
    
    UIView * line2 = [UIView new];
    line2.backgroundColor = KHEXCOLOR(@"#E0E0E0");
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.top.equalTo(self.passwordTF.mas_bottom);
        make.height.offset(0.5f);
    }];
    
    [_inviteTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(line2.mas_bottom);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(51.f);
    }];
    UIView * line3 = [UIView new];
    line3.backgroundColor = KHEXCOLOR(@"#E0E0E0");
    [self.view addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.top.equalTo(self.inviteTF.mas_bottom);
        make.height.offset(0.5f);
    }];
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(34.f));
        make.top.equalTo(line3.mas_bottom).offset(100.f);
        make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-34.f));
        make.height.offset(40.f);
    }];

    
}

#pragma mark -- UITextFiledDelegate
- (void)passwardTFStrChange:(UITextField *)textFiled{
    NSLog(@"%@",textFiled.text);
}

- (void)accountTFStrChange:(UITextField *)textFiled{
    NSLog(@"%@",textFiled.text);
}


//登录
- (void)loginTouch:(UIButton *)sender{
    [self showAlwaysTipHudWithText:@"登录中，请稍等"];
    if ([NSObject ldy_isEmpty:_accountTF.text] ) {
        [self showTipHudWithText:@"账号不能为空"];
        return;
    }else if ([NSObject ldy_isEmpty:_passwordTF.text]){
        [self showTipHudWithText:@"密码不能为空"];
        return;
    }
    [LoginRequestLogic UserLoginUserName:_accountTF.text withType:2 withPassword:_passwordTF.text userUUid:[GetUUID getUUID] invitationCode:_inviteTF.text Success:^(NSDictionary * _Nonnull Dict) {
        [self hideHud];
        NSLog(@"成功%@",Dict);
        if ([Dict[@"code"] integerValue] == 200) {
//            NSDictionary * dic = Dict[@"data"];
//            NSDictionary * userInfoDic = dic[@"userInfo"];
//            UserModel * model = [UserModel mj_objectWithKeyValues:userInfoDic];
//            model.token = dic[@"token"];
//            [[LoginManager shareLoginManager] saveLoginCustomer:model];
//            [self showTipHudWithText:Dict[@"message"]];
//            [[LoginManager shareLoginManager] pushHomeViewController];
        }else{
            [self showTipHudWithText:Dict[@"message"]];
            
        }
    } failure:^(NSString * _Nonnull errMsg) {
        [self hideHud];
        [self showTipHudWithText:@"登录失败"];
        NSLog(@"错误%@",errMsg);
    }];

}

- (void)getCode{
    [LoginRequestLogic UserLoginCode:_passwordTF.text withType:@"1" Success:^(NSDictionary * _Nonnull Dict) {
         NSLog(@"成功%@",Dict);
        if ([Dict[@"code"] integerValue] == 200) {
            [MBProgressHUD showSuccess:@"验证码发送成功" afterDelay:2];
        }
        
       
    } failure:^(NSString * _Nonnull errMsg) {
         NSLog(@"错误%@",errMsg);
    }];
}

//邀请码
- (void)inviteCodeTFStrChange:(UITextField *)textFiled{
    NSLog(@"邀请码");
}

//获取验证码
- (void)getCodeTouch:(UIButton *)sender{
    // 创建定时器
    [self getCode];
    [self startTime:60];
    //  获取验证码网络请求
    
}
//获取uuid
//- (NSString *)getIOSUUID
//{
//    NSString *retrieveuuid = [SSKeychain passwordForService:@"com.mohe.userinfo"account:@"uuid"];
//    
//    if ( retrieveuuid == nil || [retrieveuuid isEqualToString:@""]){
//        CFUUIDRef uuid = CFUUIDCreate(NULL);
//        assert(uuid != NULL);
//        CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
//        
//        retrieveuuid = [NSString stringWithFormat:@"%@", uuidStr];
//        
//        [SSKeychain setPassword:retrieveuuid forService:@"com.mohe.userinfo"account:@"uuid"];
//    }
//    return retrieveuuid;
//}

//返回
- (void)BackActive:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)startTime:(int)time{
    __block int timeout = time;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        //倒计时结束
        if(timeout <= 0){
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.getCodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
                self.getCodeBtn.userInteractionEnabled = YES;
                [self.getCodeBtn setTitleColor:KHEXCOLOR(@"#D0021B") forState:UIControlStateNormal];
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                NSString *text = [NSString stringWithFormat:@"%02d秒重新获取",timeout];
                [self.getCodeBtn setTitle:text forState:UIControlStateNormal];
                [self.getCodeBtn setTitleColor:KHEXCOLOR(@"#AFAFAF") forState:UIControlStateNormal];
                self.getCodeBtn.userInteractionEnabled = NO;
                
            });
            timeout --;
        }
    });
    dispatch_resume(_timer);
    
}



- (UIImageView *)logoImageView{
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc]init];
        _logoImageView.layer.cornerRadius = 5;
        _logoImageView.layer.masksToBounds = YES;
        _logoImageView.image = [UIImage imageNamed:@"logo"];
        //        NSData * imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"defultLogo"]];
        //        _logoImageView.image = [UIImage imageWithData:imageData];
    }
    return _logoImageView;
}

- (UITextField *)accountTF{
    if (!_accountTF) {
        _accountTF = [[UITextField alloc]init];
        _accountTF.placeholder = @"请输入手机号";
        _accountTF.font = kMainFont(15);
        _accountTF.delegate =self;
        _accountTF.textColor = KHEXCOLOR(@"#262B2F");
        _accountTF.borderStyle = UITextBorderStyleNone;
        [_accountTF addTarget:self action:@selector(accountTFStrChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _accountTF;
}

- (UITextField *)passwordTF{
    if (!_passwordTF) {
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.placeholder = @"请输入验证码";
        _passwordTF.font = kMainFont(15);
        _passwordTF.delegate =self;
        _passwordTF.textColor = KHEXCOLOR(@"#262B2F");
        _passwordTF.borderStyle = UITextBorderStyleNone;
        _passwordTF.secureTextEntry = YES;
        [_passwordTF addTarget:self action:@selector(passwardTFStrChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _passwordTF;
}

-(UIButton *)loginBtn{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setBackgroundColor:KHEXCOLOR(@"#4795EF")];
        _loginBtn.layer.cornerRadius = 5.0f;
        _loginBtn.layer.masksToBounds = YES;
        [_loginBtn setTitleColor:KHEXCOLOR(@"#ffffff") forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginTouch:) forControlEvents:UIControlEventTouchUpInside];
        [_loginBtn.titleLabel setFont:kMainFont(15)];
    }
    return _loginBtn;
}

-(UIButton *)getCodeBtn{
    if (!_getCodeBtn) {
        _getCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_getCodeBtn setBackgroundColor:KHEXCOLOR(@"#ffffff")];
        [_getCodeBtn setTitleColor:KHEXCOLOR(@"#D0021B") forState:UIControlStateNormal];
        [_getCodeBtn addTarget:self action:@selector(getCodeTouch:) forControlEvents:UIControlEventTouchUpInside];
        [_getCodeBtn.titleLabel setFont:kMainFont(13)];
    }
    return _getCodeBtn;
}

- (UITextField *)inviteTF{
    if (!_inviteTF) {
        _inviteTF = [[UITextField alloc]init];
        _inviteTF.placeholder = @"请输入邀请码（选填）";
        _inviteTF.font = kMainFont(15);
        _inviteTF.delegate =self;
        _inviteTF.textColor = KHEXCOLOR(@"#262B2F");
        _inviteTF.borderStyle = UITextBorderStyleNone;
        [_inviteTF addTarget:self action:@selector(inviteCodeTFStrChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _inviteTF;
}

- (UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:[UIImage imageNamed:@"bar_back1"] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(BackActive:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}


@end
